package com.herench.bs.transfer;

import com.herench.bs.transfer.Dialect;
import com.herench.bs.utils.DataFormatUtils;
import org.apache.cxf.endpoint.Client;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.*;

/**
 * Created by ThinkPad on 2017/9/8.
 */
public class LoadingAppointRecordTransfer extends AbstractTransfer {

    public LoadingAppointRecordTransfer(String method, Client client,String messageMethod)   {
        super(method, client,messageMethod);
    }

    @Override
    public String toInvokeMessage(Map<String, Object> params) throws Exception {
        String idCard = (String) params.get("idCard");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR, 14);
        String endTime = DataFormatUtils.getSimpleTimeString(calendar.getTime());
        calendar.add(Calendar.YEAR, -1);
        String beginTime = DataFormatUtils.getSimpleTimeString(calendar.getTime());
        Document tplDoc = getTplDoc(getMethod());
        Element dataRowElement = tplDoc.getRootElement().element("CommitData").addElement("Data").addElement("DataRow");
        dataRowElement.addAttribute("APPTED_ID", "").addAttribute("CREDENTIALS_NO", idCard).addAttribute("CREDENTIALS_TYPE", "1");
        dataRowElement.addAttribute("START_DATE", beginTime).addAttribute("END_DATE", endTime).addAttribute("DETAIL_STATUS ", "3");
        return tplDoc.asXML();
    }

    @Override
    public Map<String, Object> toResultMessage(String resMsg) throws Exception {
        Map<String, Object> resMap = new HashMap<>();
        Document document = DocumentHelper.parseText(resMsg);
        Element resultInfo = document.getRootElement().element("Result").element("Info");
        String executeStatus = resultInfo.attributeValue("EXECUTE_STATUS");
        String executeMessage = resultInfo.attributeValue("EXECUTE_MESSAGE");
        if (executeStatus.equals("0")) {
            List<Element> dataRows = document.getRootElement().element("ReturnData").element("Data").elements();
            List<Map<String,Object>> dataList = new ArrayList<>();
            if (dataRows != null && dataRows.size() > 0) {
                for (Element dataRow : dataRows) {
                    Map<String,Object> dataMap = new HashMap<>();
                    String clinicType = dataRow.attributeValue("CLINIC_TYPE");
                    dataMap.put("categorCode", Dialect.getClinicTypeCode(clinicType));
                    dataMap.put("categorName", clinicType);
                    dataMap.put("deptId", dataRow.attributeValue("DEPT_CODE"));
                    dataMap.put("deptName", dataRow.attributeValue("DEPT_NAME"));
                    dataMap.put("idCard", dataRow.attributeValue("ID_NO"));
                    dataMap.put("clinicLabel",dataRow.attributeValue("CLINIC_LABEL"));
                    dataMap.put("schDate",dataRow.attributeValue("CLINIC_VISIT_DATE"));
                    dataMap.put("timeDesc",dataRow.attributeValue("TIME_DESC"));
                    dataMap.put("period",dataRow.attributeValue("HOUR_START")+"~"+dataRow.attributeValue("HOUR_END"));
                    dataMap.put("docId",dataRow.attributeValue("DOCTOR_CODE"));
                    dataMap.put("docName",dataRow.attributeValue("DOCTOR"));
                    dataMap.put("regFee",dataRow.attributeValue("REGIST_FEE"));
                    dataMap.put("appointStatus",dataRow.attributeValue("APPOINT_STATUS"));
                    dataMap.put("appointId",dataRow.attributeValue("APPTED_ID"));
                    dataList.add(dataMap);
                }
            }
            resMap.put("list",dataList);
            resMap.put("code", executeStatus);
            resMap.put("message", executeMessage);
            return resMap;
        } else {
            resMap.put("code", executeStatus);
            resMap.put("message", executeMessage);
            return resMap;
        }
    }


}
